﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using CID.Domain.Operations;
using Newtonsoft.Json;

namespace CID.Web.Models.Operations
{
    public class ServerAssignmentViewModel : BaseViewModel<IServerAssignment>
    {
        #region Properties
        [HiddenInput(DisplayValue = false)]
        [Required(ErrorMessage = "You must provide a Server")]
        public int? ServerId { get; set; }
        public string Server { get; set; }

        [HiddenInput(DisplayValue = false)]
        [Required(ErrorMessage = "You must provide a Server Role")]
        public int? ServerRoleId { get; set; }
        public string ServerRole { get; set; }

        [HiddenInput(DisplayValue = false)]
        [Required(ErrorMessage = "You must provide an Environment")]
        public int? EnvironmentId { get; set; }
        public string Environment { get; set; }

        public bool _destroy { get; set; }
        #endregion

        public ServerAssignmentViewModel() : base() { }

        public ServerAssignmentViewModel(IServerAssignment model) : base(model)
        {
            if (model.Environment != null)
            {
                EnvironmentId = model.Environment.Id;
                Environment = model.Environment.Name;
            }

            if (model.ServerRole != null)
            {
                ServerRoleId = model.ServerRole.Id;
                ServerRole = model.ServerRole.Name;
            }

            if (model.Server != null)
            {
                ServerId = model.Server.Id;
                Server = model.Server.Name;
            }
        }

        #region Edition Helper Members

        #region ServerRole helper members
        private IEnumerable<ServerRole> availableServerRoles;

        [JsonIgnore]
        public IEnumerable<ServerRoleViewModel> AvailableServerRoles
        {
            get { return availableServerRoles.ToList().Select(x => new ServerRoleViewModel(x)); }
        }

        public void SetAvailableServerRoles(IEnumerable<ServerRole> availableServerRoles)
        {
            this.availableServerRoles = availableServerRoles;
        }
        #endregion

        #region Server helper members
        private IEnumerable<Server> availableServers;

        [JsonIgnore]
        public IEnumerable<ServerViewModel> AvailableServers
        {
            get { return availableServers.ToList().Select(x => new ServerViewModel(x)); }
        }

        public void SetAvailableServers(IEnumerable<Server> availableServers)
        {
            this.availableServers = availableServers;
        }
        #endregion

        #region Environment helper members
        private IEnumerable<Environment> availableEnvironments;

        [JsonIgnore]
        public IEnumerable<EnvironmentViewModel> AvailableEnvironments
        {
            get { return availableEnvironments.ToList().Select(x => new EnvironmentViewModel(x)); }
        }

        public void SetAvailableEnvironments(IEnumerable<Environment> availableEnvironments)
        {
            this.availableEnvironments = availableEnvironments;
        }
        #endregion
        #endregion

    }
}